<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>双色球选号</title>
</head>
<body>
    <p>选项</p>
    所有蓝球号码不重复<input type="checkbox" id="blueNotRepeat"><br>
    蓝球排除以下号码&nbsp;<button type="button" onclick="addExInput()"> + </button>
    <button type="button" onclick="clearEx()">清空</button>
    <div id="blueExclude"></div>
    <p>买多少注？</p>
    <input type="number" id="num">&nbsp;<button type="button" onclick="pick()">生成号码</button>
    <br>
    <div id="wrapper"></div>
</body>
<script>
    function clearEx() {
        let blueExclude = document.querySelector("#blueExclude");
        blueExclude.innerHTML = "";
    }

    function addExInput() {
        let blueExclude = document.querySelector("#blueExclude");
        let input = document.createElement("input");
        input.classList.add("ex");
        input.type = 'number';
        input.style.width = '30px';
        blueExclude.appendChild(input);
    }

    function pick() {
        var num = document.querySelector("#num").value;
        if (!num) {
            return ;
        }

        var wrapper = document.querySelector("#wrapper");
        wrapper.innerHTML = "";

        var blueSet = new Set();
        var exInputs = document.querySelectorAll(".ex");
        if (exInputs.length > 0) {
            for (let input of exInputs) {
                let val = input.value;
                if (!isNaN(val)) {
                    continue;
                }
                blueSet.add(parseInt(val));
            }
        }

        var blueNotRepeat = document.querySelector("#blueNotRepeat").checked;

        for (let i = 0; i < num; i++) {
            //生成6个红球号码
            let numStr = "";
            let redNumbers = new Array();
            for (let j = 0; j < 6; j++) {
                while (true) {
                    let number = parseInt(Math.random() * 33) + 1;
                    if (redNumbers.indexOf(number) == -1) {
                        redNumbers.push(number);
                        break;
                    }
                }
            }
            redNumbers.sort(function (a, b) { return a - b });
            for (let n of redNumbers) {
                numStr = numStr + (n < 10 ? "0" + n : n) + "，";
            }
            //生成一个蓝球号码
            while (true) {
                let number = parseInt(Math.random() * 16) + 1;
                if (blueSet.has(number)) {
                    continue;
                }
                numStr = numStr + (number < 10 ? "0" + number : number);
                if (blueNotRepeat) {
                    blueSet.add(number);
                }
                break;
            }
            var p = document.createElement("p");
            p.innerText = numStr;
            wrapper.appendChild(p);
        }
    }
</script>
</html>